#include "student.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

StudentList* createStudentList() {
    StudentList *list = (StudentList*)malloc(sizeof(StudentList));
    list->head = NULL;
    list->count = 0;
    return list;
}

void addStudent(StudentList *list) {
    Student newStudent;
    printf("请输入学生信息：\n");
    printf("学号: ");
    scanf("%d", &(newStudent.id));
    while(getchar() != '\n');
    printf("姓名: ");
    fgets(newStudent.name, sizeof(newStudent.name), stdin);
    newStudent.name[strlen(newStudent.name)-1] = '\0'; // remove the newline
    printf("年龄: ");
    scanf("%d", &(newStudent.age));
    while(getchar() != '\n');
    printf("性别: ");
    fgets(newStudent.gender, sizeof(newStudent.gender), stdin);
    newStudent.gender[strlen(newStudent.gender)-1] = '\0';
    printf("专业: ");
    fgets(newStudent.major, sizeof(newStudent.major), stdin);
    newStudent.major[strlen(newStudent.major)-1] = '\0';
    printf("班级: ");
    fgets(newStudent.class_name, sizeof(newStudent.class_name), stdin);
    newStudent.class_name[strlen(newStudent.class_name)-1] = '\0';
    printf("成绩: ");
    scanf("%f", &(newStudent.score));

    // add the new student to the list
    StudentNode *newNode = (StudentNode*)malloc(sizeof(StudentNode));
    newNode->data = newStudent;
    newNode->next = list->head;
    list->head = newNode;
    list->count++;

    printf("学生信息已添加成功!\n");
}

void deleteStudent(StudentList *list) {
    int id;
    printf("请输入要删除的学生的学号: ");
    scanf("%d", &id);

    StudentNode *current = list->head;
    StudentNode *previous = NULL;
    while (current != NULL) {
        if (current->data.id == id) {
            if (previous == NULL) {
                list->head = current->next;
            } else {
                previous->next = current->next;
            }
            free(current);
            list->count--;
            printf("学生信息已删除。\n");
            return;
        }
        previous = current;
        current = current->next;
    }
    printf("未找到该学生的信息。\n");
}

void modifyStudent(StudentList *list) {
    int id;
    printf("请输入要修改的学生的学号: ");
    scanf("%d", &id);

    StudentNode *current = list->head;
    while (current != NULL) {
        if (current->data.id == id) {
            printf("请输入新的学生信息：\n");
            printf("姓名: ");
            while(getchar() != '\n'); // clear buffer
            fgets(current->data.name, sizeof(current->data.name), stdin);
            current->data.name[strlen(current->data.name)-1] = '\0'; // remove newline
            printf("年龄: ");
            scanf("%d", &(current->data.age));
            printf("性别: ");
            while(getchar() != '\n');
            fgets(current->data.gender, sizeof(current->data.gender), stdin);
            current->data.gender[strlen(current->data.gender)-1] = '\0';
            printf("专业: ");
            while(getchar() != '\n');
            fgets(current->data.major, sizeof(current->data.major), stdin);
            current->data.major[strlen(current->data.major)-1] = '\0';
            printf("班级: ");
            while(getchar() != '\n');
            fgets(current->data.class_name, sizeof(current->data.class_name), stdin);
            current->data.class_name[strlen(current->data.class_name)-1] = '\0';
            printf("成绩: ");
            scanf("%f", &(current->data.score));
            printf("学生信息已修改。\n");
            return;
        }
        current = current->next;
    }
    printf("未找到该学生的信息。\n");
}

void queryStudent(const StudentList *list) {
    int id;
    printf("请输入要查询的学生的学号: ");
    scanf("%d", &id);

    StudentNode *current = list->head;
    while (current != NULL) {
        if (current->data.id == id) {
            printf("学号: %d\n", current->data.id);
            printf("姓名: %s\n", current->data.name);
            printf("年龄: %d\n", current->data.age);
            printf("性别: %s\n", current->data.gender);
            printf("专业: %s\n", current->data.major);
            printf("班级: %s\n", current->data.class_name);
            printf("成绩: %.2f\n", current->data.score);
            return;
        }
        current = current->next;
    }
    printf("未找到该学生的信息。\n");
}

void displayAllStudents(const StudentList *list) {
    if (!list->head) {
        printf("没有学生信息可供显示。\n");
        return;
    }

    printf("学号   姓名       年龄 性别 专业                  班级       成绩\n");
    printf("-----------------------------------------------------------------\n");

    StudentNode *current = list->head;
    while (current != NULL) {
        printf("%-6d %-10s %-4d %-4s %-20s %-10s %.2f\n",
               current->data.id, current->data.name, current->data.age,
               current->data.gender, current->data.major, current->data.class_name,
               current->data.score);
        current = current->next;
    }

    printf("-----------------------------------------------------------------\n");
}


// 排序学生信息（这里以学号为例，升序排列）
void sortStudents(StudentList *list) {
    // 使用冒泡排序算法
    if (list->count < 2) return;

    StudentNode *i, *j;
    for (i = list->head; i->next != NULL; i = i->next) {
        for (j = list->head; j->next != NULL; j = j->next) {
            if (j->data.id > j->next->data.id) {
                // 交换数据
                Student temp = j->data;
                j->data = j->next->data;
                j->next->data = temp;
            }
        }
    }
    printf("学生信息已排序。\n");
}

void freeStudentList(StudentList *list) {
    StudentNode *current = list->head;
    while (current != NULL) {
        StudentNode *nextNode = current->next;
        free(current);
        current = nextNode;
    }
    free(list);
}

